package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.bo.FactoryBO;
import org.csu.gp_store.entity.Factory;
import org.csu.gp_store.mapper.CompanyMapper;
import org.csu.gp_store.mapper.FactoryMapper;
import org.csu.gp_store.service.FacBusinessService;
import org.csu.gp_store.service.FactoryService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    FactoryMapper factoryMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Override
    public boolean factoryExist(@NotNull String factoryName, @NotNull Long companyID) {
        LambdaQueryWrapper<Factory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Factory::getFactoryName,factoryName);
        lambdaQueryWrapper.eq(Factory::getCompanyID,companyID);
        return factoryMapper.selectList(lambdaQueryWrapper).size()>0;
    }

    @Override
    public int insertFactory(Factory factory) {
        if(factoryExist(factory.getFactoryName(),factory.getCompanyID())){
            return 0;
        }
        return factoryMapper.insert(factory);
    }

    @Override
    public int cancelFactory(Factory factory) {
        if(factoryExist(factory.getFactoryName(),factory.getCompanyID())){
            UpdateWrapper<Factory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Factory::getFactoryID,factory.getFactoryID()).set(Factory::getStatus,3);
            return factoryMapper.update(null,updateWrapper);
        }
        return 0;
    }

    @Override
    public int changeFactory(Factory factory) {
        UpdateWrapper<Factory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Factory::getFactoryID,factory.getFactoryID());
        return factoryMapper.update(factory,updateWrapper);
    }

    @Override
    public List<Factory> getFactoryList(@NotNull Long companyID) {
        LambdaQueryWrapper<Factory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(companyMapper.selectById(companyID)==null){
            return null;
        }
        lambdaQueryWrapper.eq(Factory::getCompanyID,companyID);
        return factoryMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Factory getCertainFactory(@NotNull Long factoryID) {
        return factoryMapper.selectById(factoryID);
    }
}
